//
//  AECommunityDetailViewCell.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/20.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AECommunityDetailViewCell.h"
#import "AENewsCommentsModel.h"

@interface AECommunityDetailViewCell ()

@property (nonatomic, strong) AENewsCommentsModel *model;

@property (nonatomic, strong) CALayer *bottomLine;

/// 头像
@property (nonatomic, strong) UIImageView *headImgView;

/// 昵称
@property (nonatomic, strong) UILabel *nickNameLabel;

/// 赞
@property (nonatomic, strong) TYZImageTitleButton *praiseNumView;

@property (nonatomic, strong) UIView *praiseNumBgView;
@property (nonatomic, strong) UIButton *btnPraise;
@property (nonatomic, strong) UILabel *praiseNumLabel;

//@property (nonatomic, strong) UIButton *btn

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UILabel *datetimeLabel;

@end

@implementation AECommunityDetailViewCell

- (void)initWithSubViewCell {
    [super initWithSubViewCell];
}

- (void)initWithHeadImgView {
    if (!_headImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_header_img"];
        CGRect frame = CGRectMake(15, 8, 35, 35);
        _headImgView = [UIImageView createWithImageView:self.contentView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.layer.cornerRadius = frame.size.height * 0.5;
            imageView.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithNickNameLabel {
    if (!_nickNameLabel) {
        CGRect frame = CGRectMake(self.headImgView.right + 10, self.headImgView.top, (kScreenW - self.headImgView.right - 10 - 15) * 0.5, 18);
        _nickNameLabel = [UILabel createWithLabel:self.contentView frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#8C9197"];
        }];
    }
    if (self.model) {
        self.nickNameLabel.text = objectNull(self.model.createName);
    }
}

/*
UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
if (!_btnPraise) {
    UIImage *image = [UIImage imageNamed:@"ic_main_praise_nor"];
    UIImage *imageSel = [UIImage imageNamed:@"ic_main_praise_sel"];
    NSString *str = @"0";
    float width = [str widthForFont:font] + 10 + image.size.width;
    CGRect frame = CGRectMake(self.cycleScrollView.right - 60 - width, self.cycleScrollView.bottom + 16, width, 20);
    _btnPraise = [TYZImageTitleButton createWithImageButton:self.contentView frame:frame font:font target:self action:@selector(actionWithPraise:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
        [button setTitle:str forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#3D444D"] forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:imageSel forState:UIControlStateSelected];
        [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:8];
    }];
}
if (self.model) {
    NSString *str = [NSString stringWithFormat:@"%zd", self.model.newsLikes];
    [self.btnPraise setTitle:str forState:UIControlStateNormal];
    [self.btnPraise layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:8];
    self.btnPraise.selected = self.model.like;
}*/

- (void)initWithPraiseNumView {
    UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    UIImage *imgNor = [UIImage imageNamed:@"ic_hand_praise_nor"];
    UIImage *imgSel = [UIImage imageNamed:@"ic_hand_praise_sel"];
//    UIImage *imgNor = [UIImage imageNamed:@"ic_main_praise_nor"];
//    UIImage *imgSel = [UIImage imageNamed:@"ic_main_praise_sel"];
    if (!_praiseNumView) {
        NSString *str = @"00";
        int width = imgNor.size.width + 5 + [str widthForFont:font];
        CGRect frame = CGRectMake(kScreenW - 15 - width, self.headImgView.top, 0, 18);
        _praiseNumView = [TYZImageTitleButton createWithImageButton:self.contentView frame:frame font:font target:self action:@selector(actionWithCommentPraise:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:str forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#8C9197"] forState:UIControlStateNormal];
            [button setImage:imgNor forState:UIControlStateNormal];
            [button setImage:imgSel forState:UIControlStateSelected];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        }];
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"%zd", self.model.commentsLikes];
        int width = imgNor.size.width + 5 + [str widthForFont:font];
//        if (self.model.like) {
//            width = imgSel.size.width + 2 + [str widthForFont:font];
//        }
        self.praiseNumView.width = width;
        self.praiseNumView.left = kScreenW - 15 - width;
//        self.praiseNumView.right = 15;
        [self.praiseNumView setTitle:str forState:UIControlStateNormal];
        [self.praiseNumView layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        self.praiseNumView.selected = self.model.like;
    }
}

- (void)initWithPraiseNumBgView {
    UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    UIImage *imgNor = [UIImage imageNamed:@"ic_hand_praise_nor"];
    if (!_praiseNumBgView) {
        CGRect frame = CGRectMake(0, self.headImgView.top, 60, 20);
        _praiseNumBgView = [[UIView alloc] initWithFrame:frame];
        [self.contentView addSubview:_praiseNumBgView];
//        self.praiseNumBgView.backgroundColor = [UIColor systemPinkColor];
        
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"%zd", self.model.commentsLikes];
        float width = imgNor.size.width + 2 + [str widthForFont:font];
        self.praiseNumBgView.width = width;
        self.praiseNumBgView.left = kScreenW - 15 - width;
    }
    [self initWithPraiseNumLabel];
    [self initWithBtnPraise];
    
}

- (void)initWithPraiseNumLabel {
    UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    if (!_praiseNumLabel) {
        
        CGRect frame = CGRectMake(self.praiseNumBgView.width - 30, 0, 30, 20);
        _praiseNumLabel = [UILabel createWithLabel:self.praiseNumBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#8C9197"];
            label.textAlignment = NSTextAlignmentRight;
//            label.backgroundColor = [UIColor orangeColor];
        }];
    }
    if (self.model) {
        UIImage *imgNor = [UIImage imageNamed:@"ic_hand_praise_nor"];
        NSString *str = [NSString stringWithFormat:@"%zd", self.model.commentsLikes];
        float fontWidth = [str widthForFont:font];
//        float width = imgNor.size.width + 2 + fontWidth;
//        self.praiseNumBgView.left = kScreenW - 15 - width;
//        self.praiseNumBgView.width = width;
//        self.praiseNumLabel.left = self.btnPraise.right + 2;
//        self.praiseNumLabel.width = fontWidth;
        self.praiseNumLabel.text = str;
    }
}


- (void)initWithBtnPraise {
    UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    UIImage *imgNor = [UIImage imageNamed:@"ic_hand_praise_nor"];
    UIImage *imgSel = [UIImage imageNamed:@"ic_hand_praise_sel"];
    if (!_btnPraise) {
        CGRect frame = CGRectMake(0, 0, imgNor.size.width, imgNor.size.height);
        _btnPraise = [UIButton createWithButton:self.praiseNumBgView frame:frame font:font target:self action:@selector(actionWithCommentPraise:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:imgNor forState:UIControlStateNormal];
            [button setImage:imgSel forState:UIControlStateSelected];
        }];
    }
    if (self.model) {
        self.btnPraise.centerY = self.praiseNumLabel.centerY;
        self.btnPraise.selected = self.model.like;
        self.btnPraise.left = self.praiseNumLabel.left;
    }
}


- (void)initWithContentLabel {
    if (!_contentLabel) {
        CGRect frame = CGRectMake(self.headImgView.right + 10, self.nickNameLabel.bottom + 2, kScreenW - self.headImgView.right - 10 - 15, 0);
        _contentLabel = [UILabel createWithLabel:self.contentView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#3D444D"];
            label.numberOfLines = 0;
        }];
    }
    if (self.model) {
        _contentLabel.height = self.model.commentsHeight;
        _contentLabel.text = objectNull(self.model.comments);
    }
}

- (void)initWithDatetimeLabel {
    if (!_datetimeLabel) {
        CGRect frame = self.contentLabel.frame;
        frame.origin.y = self.contentLabel.bottom + 4;
        frame.size.height = 16;
        _datetimeLabel = [UILabel createWithLabel:self.contentView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#8C9197"];
        }];
    }
    if (self.model) {
        self.datetimeLabel.top = self.contentLabel.bottom + 4;
        self.datetimeLabel.text = [EDSUtility dateTimePraise:self.model.createTimeStr];
    }
}

- (void)initWithBottomLine {
    if (!_bottomLine) {
        CGRect frame = CGRectMake(15, 0, kScreenW - 30, 1);
        _bottomLine = [CALayer drawLine:self.contentView frame:frame lineColor:[UIColor colorWithHexString:@"#EBEBEB"]];
    }
    if (self.model) {
        self.bottomLine.top = self.model.cellHeight - 1;
    }
}

/// 评论点赞
/// - Parameter sender: sender description
- (void)actionWithCommentPraise:(id)sender {
    if (self.touchCommentPraiseBlock) {
        self.touchCommentPraiseBlock();
    }
}

- (void)updateCellData:(id)cellEntity {
    self.model = cellEntity;
    self.contentView.width = kScreenW;
    
//    NSLog(@"time=%@", [EDSUtility dateTimePraise:self.model.createTimeStr]);
    
    [self initWithHeadImgView];
    
    [self initWithNickNameLabel];
    
    [self initWithPraiseNumView];
//    [self initWithPraiseNumBgView];
    
    [self initWithContentLabel];
    
    [self initWithDatetimeLabel];
    
    [self initWithBottomLine];
}

@end
